// 配置体检套餐表相关接口
const express = require("express")
const router = express.Router();
const Response = require("../utils/Response.js");

// 引入mysql 连接池
const pool = require("../utils/db.js");
const { error } = require("console");

// 查询某个医院的体检套餐
router.get('/packageBy_hospital',(req,res) =>{
  let {hospital_id} = req.query
  let sql = 'select * from sort where hospital_id=?'
  pool.query(sql,[hospital_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 查询所有体检套餐
router.get('/packageAll',(req,res) =>{
  let sql = 'select * from sort'
  pool.query(sql,(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回客户端
    res.send(Response.ok(result))
  })
})

// 为某个医院新增体检套餐
router.post('/package/add',(req,res) =>{
  let {hospital_id,s_id,s_cover,s_title,s_details,s_price,s_time,s_needing,s_graded} = req.body
  let sql = 'insert into sort (hospital_id,s_id,s_cover,s_title,s_details,s_price,s_time,s_needing,s_graded) values (?,?,?,?,?,?,?,?,?)'
  pool.query(sql,[hospital_id,s_id,s_cover,s_title,s_details,s_price,s_time,s_needing,s_graded],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 修改某个医院的体检套餐
router.post('/package/update',(req,res) =>{
  let {s_graded,s_needing,s_title,s_cover,s_details,s_price,s_time,s_id,hospital_id} = req.body
  let sql = 'update sort set s_graded,s_needing,s_title=?,s_cover=?,s_details=?,s_price=?,s_time=? where s_id=? && hospital_id=?'
  pool.query(sql,[s_graded,s_needing,s_title,s_cover,s_details,s_price,s_time,s_id,hospital_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 删除某个医院的体检套餐
router.delete('/package/delete',(req,res) =>{
  let {s_id} = req.body
  let sql = 'delete from sort where s_id=?'
  pool.query(sql,[s_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据体检套餐分类查询体检
router.post('/package/sort',(req,res) =>{
  let {s_graded} = req.body
  let sql = 'select * from sort where s_graded=?'
  pool.query(sql,[s_graded],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result));
  })
})

// 根据价格排序
// 根据体检套餐id查询该体检套餐
router.post('/packageById',(req,res) =>{
  let {s_id} = req.body
  let sql = 'select * from sort where s_id=?'
  pool.query(sql,[s_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,err))
      throw err
    }
    
    result[0].s_needing=result[0].s_needing.split('\n');
    result[0].s_details=result[0].s_details.split(',');
    // 将结果返回给客户端
    // console.log("12121result:",);
    res.send(Response.ok(result))
  })
})



module.exports = router